<template>
	<view class="team_members_page">
		<view class="all_income_block m_b_24">
			<view class="all_income_text">工作室成员待结算总收益金额(元)</view>
			<view class="all_income_money">{{ table?.pendingSettlem?.totalAmount }}</view>
			<view class="income_detail_box">
				<view class="income_item">
					<view class="income_money">¥{{ table?.pendingSettlem?.mallAmount }}</view>
					<view class="income_name">商城推广</view>
				</view>
				<view class="income_item">
					<view class="income_money">¥{{ table?.pendingSettlem?.refereeServiceAmount }}</view>
					<view class="income_name">陪宠推广</view>
				</view>
				<view class="income_item">
					<view class="income_money">¥{{ table?.pendingSettlem?.serviceAmount }}</view>
					<view class="income_name">陪宠服务</view>
				</view>
			</view>
		</view>
		<view class="all_income_block">
			<view class="all_income_text">工作室成员已结算总收益金额(元)</view>
			<view class="all_income_money">{{ table?.settle?.totalAmount }}</view>
			<view class="income_detail_box">
				<view class="income_item">
					<view class="income_money">¥{{ table?.settle?.mallAmount }}</view>
					<view class="income_name">商城推广</view>
				</view>
				<view class="income_item">
					<view class="income_money">¥{{ table?.settle?.refereeServiceAmount }}</view>
					<view class="income_name">陪宠推广</view>
				</view>
				<view class="income_item">
					<view class="income_money">¥{{ table?.settle?.serviceAmount }}</view>
					<view class="income_name">陪宠服务</view>
				</view>
			</view>
		</view>
		<view class="all_member_income_details" v-if="table.list.length > 0">
			<view class="income_title">全部成员收益明细</view>
			<view class="all_member_income_box">
				<view class="member_income_item" v-for="item in table.list" :key="item.userId" @click="toPersonIncomeDetail(item)">
					<view class="member_income_header">
						<view class="header_right">
							<image :src="item?.user?.avatar" class="user_pic"></image>
							<view class="user_info_box">
								<view class="user_name" v-if="item?.user?.realName">{{ item?.user?.realName }}</view>
								<view class="nick_tel">
									<view class="user_nickName">{{ item?.user?.nickName }}</view>
									<view class="vertica_line"></view>
									<view class="user_tel">{{ item?.user?.phone }}</view>
								</view>
							</view>
						</view>
						<uni-icons type="right" size="24" color="#5C5E5E"></uni-icons>
					</view>
					<view class="income_block">
						<view class="income_box">
							<view class="income_money">¥{{ item?.pendingSettlemAmount }}</view>
							<view class="income_status">待结算(元)</view>
						</view>
						<view class="income_box">
							<view class="income_money">¥{{ item?.settleAmount }}</view>
							<view class="income_status">已结算(元)</view>
						</view>
					</view>
				</view>
				<uni-load-more iconType="auto" :status="table.loading" />
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import api from "@/api/index";
import { useStudioStoreToRefs } from "@/store/useStudioStore";

// 定义接口类型
interface User {
	avatar: string;
	realName: string;
	nickName: string;
	phone: string;
}

interface MemberItem {
	userId: number;
	user: User;
	pendingSettlemAmount: number;
	settleAmount: number;
}

interface ResponseData {
	total: number;
	list: MemberItem[];
	map: {
		pendingSettlem: {
			totalAmount: number;
			mallAmount: number;
			refereeServiceAmount: number;
			serviceAmount: number;
		};
		settle: {
			totalAmount: number;
			mallAmount: number;
			refereeServiceAmount: number;
			serviceAmount: number;
		};
	};
}

let { identity, studio, studioBalance } = useStudioStoreToRefs();

const table = reactive<{
	loading: "loading" | "more" | "noMore";
	list: MemberItem[];
	pendingSettlem: ResponseData["map"]["pendingSettlem"];
	settle: ResponseData["map"]["settle"];
	page: number;
	limit: number;
}>({
	loading: "more",
	list: [],
	pendingSettlem: {
		totalAmount: 0,
		mallAmount: 0,
		refereeServiceAmount: 0,
		serviceAmount: 0
	},
	settle: {
		totalAmount: 0,
		mallAmount: 0,
		refereeServiceAmount: 0,
		serviceAmount: 0
	},
	page: 1,
	limit: 20
});

// 显示加载提示
const showLoading = () => {
	uni.showLoading({
		title: "加载中"
	});
};

// 隐藏加载提示
const hideLoading = () => {
	uni.hideLoading();
};

// 显示错误提示
const showError = (message: string) => {
	uni.showToast({
		title: message,
		icon: "none"
	});
};

// 数据初始化
const onDataInit = async (option = {}) => {
	table.list = [];
	table.page = 1;
	await getData();
};

const getData = async () => {
	showLoading();
	table.loading = "loading";
	try {
		const {
			data: { total = 0, list = [], map = {} }
		} = await api<ResponseData>({
			method: "get",
			url: `/mall/studio/balanceRecord/getStudioStaffIncome`,
			data: {
				page: table.page,
				limit: table.limit,
				studioId: studio.value.id
			}
		});
		console.log(list);
		table.pendingSettlem = map?.pendingSettlem;
		table.settle = map?.settle;
		table.list = table.page === 1 ? list : table.list.concat(list);
		if (!list.length || table.list.length >= total) {
			table.loading = "noMore";
		} else {
			table.loading = "more";
		}
	} catch (error) {
		hideLoading();
		table.loading = "more";
		showError("数据加载失败，请稍后重试");
	} finally {
		hideLoading();
	}
};

const toPersonIncomeDetail = (item: MemberItem) => {
	uni.navigateTo({
		url: `/pages/income/personalIncomeStatement/index?userId=${item.userId}&studioId=${studio.value.id}`
	});
};

// 防抖处理页面下拉刷新
const onRefresh = async () => {
	console.log("onPullDownRefresh");
	if (table.loading === "loading") {
		uni.stopPullDownRefresh();
	} else {
		try {
			await onDataInit();
		} catch (error) {}
		uni.stopPullDownRefresh();
	}
};

// 防抖处理触底加载
const onLoadMore = () => {
	console.log("onReachBottom");
	if (table.loading === "noMore") return false;
	table.page++;
	getData();
};

getData();
defineExpose({ getData, onDataInit, onRefresh, onLoadMore });
</script>

<style scoped lang="scss">
.team_members_page {
	padding: 32rpx;
	box-sizing: border-box;

	.all_income_block {
		width: 100%;
		background: #ffffff;
		border-radius: 24rpx;
		padding: 48rpx 32rpx;
		box-sizing: border-box;

		.all_income_text {
			font-weight: bold;
			font-size: 26rpx;
			color: #5c5e5e;
		}

		.all_income_money {
			font-weight: bold;
			font-size: 64rpx;
			color: #1b2120;
			margin-top: 16rpx;
		}

		.income_detail_box {
			margin-top: 64rpx;
			display: flex;
			flex-direction: row;

			.income_item {
				flex: 1;
				display: flex;
				flex-direction: column;

				.income_money {
					font-weight: bold;
					font-size: 30rpx;
					color: #5c5e5e;
				}

				.income_name {
					font-weight: 400;
					font-size: 26rpx;
					color: #7c807f;
					margin-top: 16rpx;
				}
			}
		}
	}
}

.m_b_24 {
	margin-bottom: 24rpx;
}

.all_member_income_details {
	margin-top: 48rpx;

	.income_title {
		font-weight: bold;
		font-size: 36rpx;
		color: #1a1a1a;
	}

	.all_member_income_box {
		margin-top: 32rpx;
		padding-bottom: calc(var(--safe-area-bottom));

		.member_income_item {
			width: 100%;
			background: #ffffff;
			border-radius: 24rpx;
			padding: 32rpx;
			box-sizing: border-box;
			margin-bottom: 24rpx;

			.member_income_header {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.header_right {
					display: flex;
					align-items: center;

					.user_info_box {
						display: flex;
						flex-direction: column;
						margin-left: 8rpx;
					}

					.user_pic {
						width: 64rpx;
						height: 64rpx;
						border-radius: 50%;
					}

					.user_name {
						font-weight: bold;
						font-size: 32rpx;
						color: #1b2120;
						margin-bottom: 8rpx;
					}

					.nick_tel {
						display: flex;
						flex-direction: row;
						align-items: center;
					}

					.vertica_line {
						width: 1rpx;
						height: 24rpx;
						background: #cacccc;
						margin: 0 8rpx;
					}

					.user_nickName {
						font-weight: 400;
						font-size: 26rpx;
						color: #7c807f;
					}

					.user_tel {
						font-weight: 400;
						font-size: 26rpx;
						color: #7c807f;
					}
				}

				uni-icons {
				}
			}

			.income_block {
				margin-top: 16rpx;
				display: flex;
				justify-content: space-between;

				.income_box {
					display: flex;
					flex: 1;
					background: #f9f6f6;
					border-radius: 24rpx;
					text-align: center;
					padding: 32rpx 0;
					flex-direction: column;
					box-sizing: border-box;
					margin-left: 4rpx;

					.income_money {
						font-weight: bold;
						font-size: 30rpx;
						color: #1b2120;
					}

					.income_status {
						font-weight: 400;
						font-size: 26rpx;
						color: #7c807f;
						margin-top: 16rpx;
					}
				}
			}
		}
	}
}
</style>
